$(function(){
	var $body = $('body');
	$('html').css('font-size', $body.outerHeight()/504*20);
	$body.height($body.outerHeight());
	
//	$body.width(Math.min($body.width(),$body.height()*320/504));
//	$body.height(Math.min($body.height(),$body.width()*504/320));
	
//	$('.app>div').css('transform','translateY(-100%)');
//	$('.lottery .pop').hide();
//	$('.lottery .main>div').css('transform','scale(1)');
	var bingoaward=false;
	
// 首页--------------------
	//100%中奖动画
	setTimeout(function(){
		$('.homepage .bingo').removeClass('zoomIn').addClass('tada').css('animation-delay','0s');
	},5000);
	
	//点击我要抽奖切换下一页
	$('.homepage .award').on('click',function(){
		$('.app>div').css('transform','translateY(-100%)');
		$('.lottery .music').removeClass('icon-disable').addClass('icon-music');
		$('.lottery audio')[0].play();
//		if(localStorage.getItem('come')){
//			$('.lottery .pop').hide();
//			$('.lottery .main>div').css('transform','scale(1)');
//			localStorage.removeItem('come');
//		}else{
//			localStorage.setItem('come','yes');
//		}
	});
	

// 抽奖--------------------
	//关闭显示抽奖规则
	$('.lottery .close').on('click',function(){
		$('.lottery .rule').css('transform','translateY(-10%)');
		$('.lottery .pop').fadeOut();
		if(!bingoaward){
			$('.lottery .main>div').css('transform','scale(1)');
		}
	});
	$('.lottery .info').on('click',function(){
		$('.lottery .rule').css('transform','translateY(0)');
		$('.lottery .pop').fadeIn();
	});
	
	//暂停播放音乐
	$('.lottery .music').on('click',function(){
		if($(this).hasClass('icon-music')){
			$(this).removeClass('icon-music').addClass('icon-disable');
			$('.lottery audio')[0].pause();
		}else{
			$(this).removeClass('icon-disable').addClass('icon-music');
			$('.lottery audio')[0].load();
			$('.lottery audio')[0].play();
		}
	});
	
	//点击抽奖
	$('.lottery .start div').on('click',function(){
		$(this).find('img').removeClass('awardgo');
		$(this).next('span').show();
		bingo=parseInt(Math.random()*8)+1;
		rotate();
		//$.ajax({
			//type: "GET", 				//请求的方式
			//url: "http://student.bluej.cn/index/index/lucky_draw", //请求的路径地址
			//async: true, 				//是否异步
			//dataType: "jsonp", 			//dataType设置为jsonp可以解决跨域问题
			//jsonp: "callback", 			//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
			//jsonpCallback: "lucky_num", //自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
			//success: function(res) { 	//请求成功并返回数据的回调
				//后面任何基于res数据的代码都要写在success的回调函数里面
				//console.log(res);
				//bingo=res[0].num+1;
				//rotate();
			//},
			//error: function(a, b, c) {
				//console.log(a); //错误对象
				//console.log(b); //错误类型
				//console.log(c); //错误的具体信息
			//}
		//});
	});
	
	//开始抽奖
	var count     = 8,		//总共的奖品个数
		timer     = null,	//定时器
		cycle     = 50,		//最起码要转多少次
		lastTimes = 0,		//最后起码再转一圈
		bingo     = 0,		//中奖的位置	
			
		index     = 1,		//当前位置
		times     = 0,		//当前转动次数
		speed     = 1000;	//转动的初始速度
	
	function rotate(){
		$('[data-self]').removeClass('award');
		$('[data-self='+index+']').addClass('award');
		
		if(times<=cycle){			//速度越来越快
				speed<400?speed-=50:speed-=100;	
		}else{						//慢慢减速
			speed+=50;
			lastTimes++;
		}
		speed=Math.max(speed,50);	//速度最快为50ms
		
		if(times>cycle&&index==bingo&&lastTimes>=count){
			clearTimeout(timer);
			$('[data-self='+index+']').removeClass('award').addClass('bingo');
			bingoa();
		}else{
			timer=setTimeout(rotate,speed);
		}
		index==count?index=1:index++;
		times++;
		//console.log(index,bingo);
	}
	
	//恭喜中奖
	function bingoa(){
		bingoaward=true;
		var offleft = $('.lottery .main').width()/2-$('.lottery .bingo').width()/2-($('.lottery .bingo').offset().left-$body.offset().left),
			offtop  = $('.lottery .main').height()/2-$('.lottery .bingo').height()/2-($('.lottery .bingo').offset().top-$('.lottery .menu').height())+10;
		$('.lottery .main>div:not([class="bingo"])').css('transform','scale(0)').on('transitionend',function(){
			$('.lottery .bingo').removeClass('bingo').css('transform','translate('+offleft+'px,'+offtop+'px)').on('transitionend',function(){
				$(this).css('transform','translate('+offleft+'px,'+offtop+'px) scale(1.3)').on('transitionend',function(){
					$('.lottery .cong,.lottery .goredeem').show().addClass('flipInY');
				});
			});
		});
	}
	
	//点击马上兑奖
	$('.lottery .goredeem').on('click',function(){
		$('.lottery .redeem').css('transform','translate(0,-100%)');
	});
	
	//点击下一步
	$('.lottery .next').on('click',function(){
		var $input=$('.lottery input').val();
		if($input==''||!/^1\d{10}$/.test($input)){
			$('.lottery .tips').finish().fadeIn().delay(500).fadeOut();
		}else{
			$('.lottery .redeem .one').css('transform','translateX(-120%)');
			$('.lottery .redeem .two').css('transform','translate(0,-100%)');
		}
	});
	
	//点击谢谢参与
	$('.lottery .thanks').on('click',function(){
		alert('thank you see');
	});
});